let logger = require("pomelo-logger").getLogger(__filename);
const HEADLINE_COLLECTION_NAME = "headlines";

let Dao = function (app) {
    this.app = app;
};

Dao.prototype.findHeadlineById = function (id, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(HEADLINE_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`find headline failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.findOne({id : id}, function (error, document) {
            if(!!error) {
                logger.error(`findHeadlineById in headlines failed. error=${error}`);
                callback(null);
                return;
            }
            callback(document);
        });
    });
};

Dao.prototype.findAllHeadlines = function (callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(HEADLINE_COLLECTION_NAME,  (error, collection) => {
        if(!!error) {
            logger.error(`find headline in headlines failed. get collection error. error=${error}`);
            callback([]);
            return;
        }
        collection.find({}).toArray(function (error, documents) {
            if(!!error){
                logger.error(`findAllHeadlines in headlines failed. error=${error}`);
                callback([]);
                return;
            }
            callback(documents);
        });
    });
};

Dao.prototype.addHeadline = function (document, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(HEADLINE_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`insert Headline in Headline failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.insertOne(document, function (error, result) {
            if (!!error) {
                logger.error(`insert Headline in Headline failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};

Dao.prototype.updateHeadline = function (id, content, startTime, endTime, frequency, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(HEADLINE_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`update headline failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.updateOne({id : id}, {$set:{content: content, startTime: startTime, endTime: endTime, frequency: frequency}},
            function (error, result) {
                if (!!error) {
                    logger.error(`update headline failed. error=${error}`);
                    callback(null);
                    return;
                }
                callback(result);
            });
    });
};

Dao.prototype.deleteHeadline = function (id, callback) {
    let dbClient = this.app.get("gameDBClient");
    dbClient.collection(HEADLINE_COLLECTION_NAME, function (error, collection) {
        if(!!error) {
            logger.error(`delete item in headline failed. get collection error. error=${error}`);
            callback(null);
            return;
        }
        collection.findOneAndDelete({id : id}, function (error, result) {
            if (!!error) {
                logger.error(`delete item in headline failed. error=${error}`);
                callback(null);
                return;
            }
            callback(result);
        });
    });
};


module.exports.create = function (app) {
    return new Dao(app);
};
